using System;
using PluginFrame;

namespace PcrDataAnalysis
{
	public class RealTimeBaseLineNormalization : ManualBaseLineNormalization, IBaseLineNormalization
	{
		public RealTimeBaseLineNormalization(IApplication app)
			: base(app)
		{
			mRotateCurve = true;
		}

		public new void Execute(IPcrCell pcrCell, IAbsoluteAnalysisParameterConfiguration analysisOption, int channelIndex, bool isAnalyzing)
		{
			IPcrExperiment pcrExperiment = pcrCell.PcrExperiment;
			if (pcrExperiment == null)
			{
				return;
			}
			double[] array;
			int num;
			double num2;
			if (pcrCell.GetBaseLineAvgValue(channelIndex) == 0.0)
			{
				array = pcrCell.GetChannelRawData(channelIndex, true);
				num = 1;
				if (array.Length < analysisOption.StopBaseline)
				{
					return;
				}
				num2 = CalculateBaseLineAverage(array, analysisOption.StartBaseline, analysisOption.StopBaseline);
				pcrCell.SetBaseLineAvgValue(channelIndex, num2);
				mBaseLineMidPoint = 0.0;
			}
			else
			{
				num = (pcrExperiment.RunningStatus.IsRestart ? 1 : pcrExperiment.RunningStatus.DetectCycle);
				array = new double[1] { pcrCell.GetRawData(channelIndex, num) };
				num2 = pcrCell.GetBaseLineAvgValue(channelIndex);
			}
			for (int i = 0; i < array.Length; i++)
			{
				if (!double.IsNaN(array[i]))
				{
					double a = array[i] / num2;
					a = Math.Log(a, 2.0);
					pcrCell.SetNormalLightData(channelIndex, i + num, a);
				}
			}
			RotateTranlateData(pcrCell, num, array, channelIndex, analysisOption.StartBaseline, analysisOption.StopBaseline);
		}

		protected double CalculateBaseLineAverage(IPcrCell pcrCell, int startBaseLine, int stopBaseLine, int channelIndex)
		{
			double num = 0.0;
			double num2 = 0.0;
			for (int i = startBaseLine; i <= stopBaseLine; i++)
			{
				num += ((!double.IsNaN(pcrCell.GetNormalLightData(channelIndex, i - 1))) ? pcrCell.GetNormalLightData(channelIndex, i - 1) : 0.0);
				num2 += 1.0;
			}
			return num / num2;
		}

		protected void RotateTranlateData(IPcrCell pcrCell, int cycle, double[] channelLightData, int channelIndex, int startBaseLine, int stopBaseLine)
		{
			if (mBaseLineMidPoint == 0.0)
			{
				double value = CalculateBaseLineAverage(pcrCell, startBaseLine, stopBaseLine, channelIndex);
				pcrCell.SetNormalBaseLineAvgValue(channelIndex, value);
				mBaseLineOptK = (pcrCell.GetNormalLightData(channelIndex, stopBaseLine) - pcrCell.GetNormalLightData(channelIndex, startBaseLine)) / (double)(stopBaseLine - startBaseLine + 1);
				mBaseLineMidPoint = (double)(stopBaseLine + startBaseLine) / 2.0;
			}
			for (int i = 0; i < channelLightData.Length; i++)
			{
				double normalLightData = pcrCell.GetNormalLightData(channelIndex, i + cycle);
				if (!double.IsNaN(normalLightData))
				{
					normalLightData = pcrCell.GetNormalLightData(channelIndex, i + cycle) - ((double)(i + 1) - mBaseLineMidPoint) * mBaseLineOptK - pcrCell.GetNormalBaseLineAvgValue(channelIndex);
					double num = 1.0;
					pcrCell.SetNormalLightData(channelIndex, i + cycle, normalLightData);
					pcrCell.SetCurveLightData(channelIndex, i + cycle, normalLightData);
				}
			}
		}
	}
}
